import { Component, Input, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { myValidNull } from '@core/util/string-utils';
import { TriggerService } from '@core/routes-services/trigger.service';
import { NzMessageService } from 'ng-zorro-antd';
import { TriggerEditComponent } from '../trigger-edit/trigger-edit.component';
import { TRIGGER_TYPE } from '../../../constant/dictionary';

@Component({
  selector: 'app-trigger-list',
  templateUrl: './trigger-list.component.html',
})
export class TriggerListComponent implements OnInit {

  @Input() triggerId:any;
  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  triggerType:any[] =TRIGGER_TYPE;
  constructor(private message: NzMessageService,
              private triggerService:TriggerService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  initPage() {
    this.isLoading = true;
    this.triggerService.pagination(this.page, this.keyword, this.triggerId).then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(TriggerEditComponent, { entity: bak,triggerId:this.triggerId }, 600, {
      nzTitle: '奖金自动发放器信息',
    }).subscribe(result => {
      if(result){
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  /**
   * 停用启用
   * @param data
   */
  updateStatus = (data) => {
    this.triggerService.updateStatus(data.id, { status: data.status == 1 ? 2 : 1 }).then((data) => {
      this.changePageNo(this.page.number);
      this.message.success('修改成功');
    }, error => console.log('修改出错，后续处理'));
  };


}
